package neatlogic.module.change.stephandler.utilhandler;

import com.alibaba.fastjson.JSON;
import neatlogic.framework.change.constvalue.ChangeOperationType;
import neatlogic.framework.change.constvalue.ChangeProcessStepHandlerType;
import neatlogic.framework.change.constvalue.ChangeStatus;
import neatlogic.framework.change.dto.ChangeStatusVo;
import neatlogic.framework.change.dto.ChangeStepVo;
import neatlogic.framework.change.dto.ChangeVo;
import neatlogic.framework.common.dto.ValueTextVo;
import neatlogic.framework.notify.core.INotifyPolicyHandler;
import neatlogic.framework.process.constvalue.ProcessTaskOperationType;
import neatlogic.framework.process.constvalue.ProcessTaskStepOperationType;
import neatlogic.framework.process.dto.ProcessTaskStepVo;
import neatlogic.framework.process.operationauth.core.IOperationType;
import neatlogic.framework.process.stephandler.core.ProcessStepInternalHandlerBase;
import neatlogic.module.change.dao.mapper.ChangeMapper;
import neatlogic.module.change.notify.handler.ChangeCreateNotifyPolicyHandler;
import neatlogic.module.change.service.ChangeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ChangeCreateProcessUtilHandler extends ProcessStepInternalHandlerBase {

    @Autowired
    private ChangeMapper changeMapper;

    @Autowired
    private ChangeService changeService;

    @Override
    public String getHandler() {
        return ChangeProcessStepHandlerType.CHANGECREATE.getHandler();
    }

    @Override
    public Object getStartStepInfo(ProcessTaskStepVo currentProcessTaskStepVo) {
        Long changeId = changeMapper.getChangeIdByProcessTaskStepId(currentProcessTaskStepVo.getId());
        if (changeId != null && changeMapper.checkChangeIsExists(changeId) > 0) {
            Map<String, String> customButtonMap = getCustomButtonMapByProcessTaskStepId(currentProcessTaskStepVo.getId());
            ChangeVo changeVo = changeService.getChangeById(changeId);
            if (changeService.isBatchUpdateChangeStepWorker(changeVo)) {
                String text = customButtonMap.get(ChangeOperationType.BATCHEDITCHANGESTEPWORKER.getValue());
                if (StringUtils.isBlank(text)) {
                    text = ChangeOperationType.BATCHEDITCHANGESTEPWORKER.getText();
                }
                changeVo.getActionList().add(new ValueTextVo(ChangeOperationType.BATCHEDITCHANGESTEPWORKER.getValue(), text));
            }
            if (changeService.isEditableChange(changeVo)) {
                String text = customButtonMap.get(ChangeOperationType.EDITCHANGE.getValue());
                if (StringUtils.isBlank(text)) {
                    text = ChangeOperationType.EDITCHANGE.getText();
                }
                changeVo.getActionList().add(new ValueTextVo(ChangeOperationType.EDITCHANGE.getValue(), text));
            }
            for (ChangeStepVo changeStepVo : changeVo.getChangeStepList()) {
                if (changeService.isEditableChangeStep(changeVo, changeStepVo)) {
                    String text = customButtonMap.get(ChangeOperationType.EDITCHANGESTEP.getValue());
                    if (StringUtils.isBlank(text)) {
                        text = ChangeOperationType.EDITCHANGESTEP.getText();
                    }
                    changeStepVo.getActionList().add(new ValueTextVo(ChangeOperationType.EDITCHANGESTEP.getValue(), text));
                }
            }
            List<ChangeStatusVo> changeStatusVoList = new ArrayList<>();
            changeStatusVoList.add(new ChangeStatusVo(ChangeStatus.PENDING.getValue()));
            changeStatusVoList.add(new ChangeStatusVo(ChangeStatus.RUNNING.getValue()));
            changeStatusVoList.add(new ChangeStatusVo(ChangeStatus.SUCCEED.getValue()));
            changeStatusVoList.add(new ChangeStatusVo(ChangeStatus.ABORTED.getValue()));
            changeVo.setChangeStatusVoList(changeStatusVoList);
            return changeVo;
        }
        return null;
    }

    @Override
    public Object getNonStartStepInfo(ProcessTaskStepVo currentProcessTaskStepVo) {
        String changeConfig = changeMapper.getChangeConfigByProcessTaskStepId(currentProcessTaskStepVo.getId());
        if (StringUtils.isNotBlank(changeConfig)) {
            return JSON.parseObject(changeConfig);
        }
        return null;
    }

    @Override
    public void updateProcessTaskStepUserAndWorker(Long processTaskId, Long processTaskStepId) {

    }

    public IOperationType[] getStepActions() {
        return new IOperationType[]{
                ProcessTaskStepOperationType.STEP_VIEW,
                ProcessTaskStepOperationType.STEP_TRANSFER,
                ProcessTaskStepOperationType.STEP_RETREAT
        };
    }

    @Override
    public IOperationType[] getStepButtons() {
        return new IOperationType[]{
                ProcessTaskStepOperationType.STEP_COMPLETE,
                ProcessTaskStepOperationType.STEP_BACK,
                ProcessTaskStepOperationType.STEP_COMMENT,
                ProcessTaskOperationType.PROCESSTASK_TRANSFER,
                ProcessTaskOperationType.PROCESSTASK_ABORT,
                ProcessTaskOperationType.PROCESSTASK_RECOVER
        };
    }

    @Override
    public Class<? extends INotifyPolicyHandler> getNotifyPolicyHandlerClass() {
        return ChangeCreateNotifyPolicyHandler.class;
    }

    @Override
    public String[] getRegulateKeyList() {
        return new String[]{"authorityList", "notifyPolicyConfig", "actionConfig", "customButtonList", "customStatusList", "replaceableTextList", "workerPolicyConfig", "formSceneUuid", "formSceneName", "autoStart", "isNeedUploadFile", "isNeedContent", "isRequired", "commentTemplateId", "tagList"};
    }

}
